package com.sdry.web.controller.zc.quality;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.Team;
import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcProductInspectionEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.model.zc.ZcTrolleyBindingEntity;
import com.sdry.service.jyy.ShedudetailServise;
import com.sdry.service.zc.ZcProductInspectionService;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcProductInspectionController   
 * @Description:  台车绑定与产品报检
 * @Author:       zc   
 * @CreateDate:   2018年11月8日 上午11:24:37   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("productInspection")
public class ZcProductInspectionController {

	@Resource
	private ZcProductInspectionService zcProductInspectionService;
	
	@Resource
	private ShedudetailServise shedudetailServise;
	
	/**
	 * 进入产品报检页面
	 * @return
	 */
	@RequestMapping("enterProductInspectionPage")
	public String enterProductInspectionPage(){
		return "/zc/quality/productInspection";
	}
	/**
	 * 查询绑定列表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectTrolleyBindingList")
	public void selectTrolleyBindingList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcTrolleyBindingEntity> trolleyBindingList = zcProductInspectionService.selectTrolleyBindingList(zcQueryCriteria);
		int count = zcProductInspectionService.countTrolleyBindingList();
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(trolleyBindingList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 台车绑定
	 * @param request
	 * @param response
	 * @param zcTrolleyBindingEntity
	 */
	@RequestMapping("addTrolleyBinding")
	public void addTrolleyBinding(HttpSession httpSession, HttpServletRequest request,HttpServletResponse response,ZcTrolleyBindingEntity zcTrolleyBindingEntity){
		//jyy 2018-12-21 10:13:37
		Team team = (Team) httpSession.getAttribute("team");
		if(team != null) {
			if(team.getTname() != null && team.getTname() != "") {
				String tname = team.getTname();
				//获取当前时间
				String dateFormat1 = DateUtil.dateFormat1();
				
				team.setRemark01(dateFormat1);
				//根据当前时间  当前班组id 查询排产单号
				String remark02 = shedudetailServise.selectDaysheduTime(team);
				zcTrolleyBindingEntity.setRemark02(remark02);
				//当前班组
				zcTrolleyBindingEntity.setRemark03(tname);
				
			}
		}
		//jyy 修改结束
		int affact = zcProductInspectionService.addTrolleyBinding(zcTrolleyBindingEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 解除台车绑定
	 * @param request
	 * @param response
	 * @param tId
	 */
	@RequestMapping("deleteTrolleyBinding")
	public void deleteTrolleyBinding(HttpServletRequest request,HttpServletResponse response,long tId){
		int affact = zcProductInspectionService.deleteTrolleyBinding(tId);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 修改台车绑定
	 * @param request
	 * @param response
	 * @param zcTrolleyBindingEntity
	 */
	@RequestMapping("editTrolleyBinding")
	public void editTrolleyBinding(HttpServletRequest request,HttpServletResponse response,ZcTrolleyBindingEntity zcTrolleyBindingEntity){
		int affact = zcProductInspectionService.editTrolleyBinding(zcTrolleyBindingEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 报检
	 * @param request
	 * @param response
	 * @param ZcProductInspectionEntity
	 */
	@RequestMapping("productInspection")
	public void productInspection(HttpServletRequest request,HttpServletResponse response,ZcProductInspectionEntity zcProductInspectionEntity,HttpSession httpSession){
		
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcProductInspectionEntity.setPiPerson(userName);
		Date date = new Date();
		zcProductInspectionEntity.setPiDate(date);
		zcProductInspectionEntity.setPiStatus("0");
		int affact = zcProductInspectionService.productInspection(zcProductInspectionEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
